﻿//========================================
// All rights reserved (C) 2017-2100
// CLR 版本：	4.0.30319.42000
// 命名空间:	SFJDManagement
// 文 件 名：	HtmlPrint1
// 作    者：	WooG
// 联系方式：	wooglle@163.com
// 创建时间：	2017/11/7 22:38:36
//========================================
using Common;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;

namespace SFJDManagement
{
    class HtmlPrint1
    {
        private StringBuilder strHTMLBuilder = new StringBuilder();

        public HtmlPrint1(string str, DataGrid dataGrid)
        {
            Ini(str);
            GenerateByDataGrid(str, dataGrid);
        }

        private void Ini(string str)
        {
            add("<html>");
            add("<head>");
            add("<meta charset='utf-8'>");
            add("<title>打印预览</title>");
            add("<style type='text/css'>");
            add("h1,h2,h3{text-align:center;font-family:'微软雅黑';line-height:16px;}");
            add("table,table tr th,table tr td{border:1px solid;}");
            add("table thead{display:table-header-group;font-weight:bold}");
            add("table{margin:0 auto; border-collapse:collapse; font-family:仿宋; font-size:smaller}");
            add("</style>");
            add("</head>");
            add("<body>");
            add("<h2>" + Runtime.institution + "</h2>");
            add("<h2>" + "交通事故车辆鉴定明细表" + "</h2>");
            add("<h3>（" + str + "）</h3>");
        }

        private void GenerateByDataGrid(string str, DataGrid dataGrid)
        {
            int subtotal, total = 0;

            add("<table>");
            add("<thead><tr>");
            add("<tr><th rowspan='2'>肇事时间</th>");
            add("<th colspan='2'>车辆信息</th>");
            add("<th colspan='4'>委托项目</th>");
            add("<th rowspan='2'>金额</th>");
            add("<th rowspan='2'>委托民警</th>");
            add("<th rowspan='2'>车鉴字号</th>");
            add("<th rowspan='2'>委托日期</th>");
            add("<tr><th>车号</th>");
            add("<th>车型</th>");
            add("<th>车检</th>");
            add("<th>车速</th>");
            add("<th>痕迹</th>");
            add("<th>属性</th>");
            add("</tr>");

            add("</tr></thead>");
            add("<tbody>");
            add("<tfoot>");
            add("</tfoot>");
            for(int i =0; i < dataGrid.Items.Count; i++)
            {
                subtotal = 0;
                DataGridRow gridRow = dataGrid.GetRow(i);
                if(gridRow.Visibility == System.Windows.Visibility.Visible)
                {
                    ClassData data = gridRow.DataContext as ClassData;
                    subtotal = ComputeSubtotal(data.Vehicles);
                    total += subtotal;
                    int vehicleCount = data.Vehicles.Count;
                    add("<tr>");
                    add("<td rowspan='" + vehicleCount + "'>" + data.TimeHappen.ToString("yyyy-MM-dd") + "</td>");
                    ClassVehicle vehicle0 = data.Vehicles[0];
                    AddVehicle(vehicle0);

                    add("<td rowspan='" + vehicleCount + "'>" + subtotal + "</td>");
                    add("<td rowspan='" + vehicleCount + "'>" + data.Contact + "</td>");
                    add("<td rowspan='" + vehicleCount + "'>【" + data.TimeArrange.ToString("yyyy") + "】第" + data.ID + "号</td>");
                    add("<td rowspan='" + vehicleCount + "'>" + data.TimeEntrust.ToString("yyyy-MM-dd") + "</td>");
                    for (int j = 1; j < vehicleCount; j++)
                    {
                        add("<tr>");
                        AddVehicle(data.Vehicles[j]);
                        add("</tr>");
                    }
                    add("</tr>");
                }
            }
            add("<tr><td colspan='11'>总计：" + total + "</td></tr>");
            
            add("</tbody>");
            add("</table>");
            add("</body>");
            add("</html>");
        }

        private int ComputeSubtotal(List<ClassVehicle> list)
        {
            int b = 0;
            foreach(ClassVehicle vehicle in list)
            {
                if (vehicle.Procj)
                    b += 350;
                if (vehicle.Procs)
                    b += 1000;
                if (vehicle.Prohj | vehicle.Projsy | vehicle.Procy | vehicle.Progc | vehicle.Projsy)
                    b += 1000;
                if (vehicle.Prosx)
                    b += 600;
            }
            return b;
        }

        private void AddVehicle(ClassVehicle vehicle)
        {
            add("<td>" + vehicle.Number + "</td>");
            VehicleType type = new VehicleType(vehicle.Type);
            add("<td>" + type.fullName + "</td>");
            if (vehicle.Procj)
            {
                add("<td>350</td>");
            }
            else
            {
                add("<td></td>");
            }
            if (vehicle.Procs)
            {
                add("<td>1000</td>");
            }
            else
            {
                add("<td></td>");
            }
            if (vehicle.Prohj | vehicle.Projsy | vehicle.Procy | vehicle.Progc | vehicle.Projsy)
            {
                add("<td>1000</td>");
            }
            else
            {
                add("<td></td>");
            }
            if (vehicle.Prosx)
            {
                add("<td>600</td>");
            }
            else
            {
                add("<td></td>");
            }
        }

        private void add(string str)
        {
            strHTMLBuilder.Append(str);
        }

        public string GetHtml()
        {
            return strHTMLBuilder.ToString();
        }
    }
}
